package com.longrou.base.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;

import com.longrou.base.R;

/**
 * @Description:
 * @Author: LongRou
 * @CreateDate: 2018-05-17 23:39
 * @Version: 1.0
 **/
public class J4LinearLayout extends LinearLayout {

    //corner
    private float mCornerRadius;
    private float mCornerRadiusTopLeft;
    private float mCornerRadiusTopRight;
    private float mCornerRadiusBottomLeft;
    private float mCornerRadiusBottomRight;

    //BorderWidth
    private float mBorderDashWidth = 0;
    private float mBorderDashGap = 0;
    private int mBorderWidthNormal;
    private int mBorderWidthPressed;
    private int mBorderWidthUnable;

    //BorderColor
    private int mBorderColorNormal;
    private int mBorderColorPressed;
    private int mBorderColorUnable;

    //Background
    private int mBackgroundColorNormal;
    private int mBackgroundColorPressed;
    private int mBackgroundColorUnable;
    private GradientDrawable mBackgroundNormal;
    private GradientDrawable mBackgroundPressed;
    private GradientDrawable mBackgroundUnable;

    private int[][] states = new int[4][];
    private StateListDrawable mStateBackground;
    private float mBorderRadii[] = new float[8];

    /**
     * Cache the touch slop from the context that created the view.
     */
    private int mTouchSlop;

    public J4LinearLayout(Context context) {
        this(context, null);
    }

    public J4LinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        initAttributeSet(context, attrs);
    }




    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            //按下
            case MotionEvent.ACTION_DOWN:
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       /* if (mIconPressed != null) {
                            mIcon = mIconPressed;
                            setIcon();
                        }*/
                    }
                }, ViewConfiguration.getTapTimeout());
                break;
            //抬起    
            case MotionEvent.ACTION_UP:
                /*if (mIconNormal != null) {
                    mIcon = mIconNormal;
                    setIcon();
                }*/
                break;
            //移动    
            case MotionEvent.ACTION_MOVE:
                int x = (int) event.getX();
                int y = (int) event.getY();
                /*if (isOutsideView(x, y)) {
                    if (mIconNormal != null) {
                        mIcon = mIconNormal;
                        setIcon();
                    }
                }*/
                break;
            //父级控件获取控制权    
            case MotionEvent.ACTION_CANCEL:
              /*  if (mIconNormal != null) {
                    mIcon = mIconNormal;
                    setIcon();
                }*/
                break;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 初始化控件属性
     *
     * @param context
     * @param attrs
     */
    private void initAttributeSet(Context context, AttributeSet attrs) {
        if (context == null || attrs == null) {
            return;
        }
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.J4LinearLayout);
        //corner
        mCornerRadius = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_corner_radius, -1);
        mCornerRadiusTopLeft = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_corner_radius_top_left, 0);
        mCornerRadiusTopRight = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_corner_radius_top_right, 0);
        mCornerRadiusBottomLeft = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_corner_radius_bottom_left, 0);
        mCornerRadiusBottomRight = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_corner_radius_bottom_right, 0);
        //border
        mBorderDashWidth = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_border_dash_width, 0);
        mBorderDashGap = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_border_dash_gap, 0);
        mBorderWidthNormal = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_border_width_normal, 0);
        mBorderWidthPressed = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_border_width_pressed, 0);
        mBorderWidthUnable = a.getDimensionPixelSize(R.styleable.J4LinearLayout_JLL_border_width_unable, 0);
        mBorderColorNormal = a.getColor(R.styleable.J4LinearLayout_JLL_border_color_normal, Color.TRANSPARENT);
        mBorderColorPressed = a.getColor(R.styleable.J4LinearLayout_JLL_border_color_pressed, Color.TRANSPARENT);
        mBorderColorUnable = a.getColor(R.styleable.J4LinearLayout_JLL_border_color_unable, Color.TRANSPARENT);

        //background
        mBackgroundColorNormal = a.getColor(R.styleable.J4LinearLayout_JLL_background_normal, 0);
        mBackgroundColorPressed = a.getColor(R.styleable.J4LinearLayout_JLL_background_pressed, 0);
        mBackgroundColorUnable = a.getColor(R.styleable.J4LinearLayout_JLL_background_unable, 0);

        mBackgroundNormal = new GradientDrawable();
        mBackgroundPressed = new GradientDrawable();
        mBackgroundUnable = new GradientDrawable();

        mBackgroundNormal.setColor(mBackgroundColorNormal);
        mBackgroundPressed.setColor(mBackgroundColorPressed);
        mBackgroundUnable.setColor(mBackgroundColorUnable);

        a.recycle();

        //setup
        setup();

    }

    /**
     * 设置
     */
    private void setup() {

        Drawable drawable = getBackground();
        if (drawable != null && drawable instanceof StateListDrawable) {
            mStateBackground = (StateListDrawable) drawable;
        } else {
            mStateBackground = new StateListDrawable();
        }

        //pressed, focused, normal, unable
        states[0] = new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed};
        states[1] = new int[]{android.R.attr.state_enabled, android.R.attr.state_focused};
        states[3] = new int[]{-android.R.attr.state_enabled};
        states[2] = new int[]{android.R.attr.state_enabled};
        mStateBackground.addState(states[0], mBackgroundPressed);
        mStateBackground.addState(states[1], mBackgroundPressed);
        mStateBackground.addState(states[3], mBackgroundUnable);
        mStateBackground.addState(states[2], mBackgroundNormal);



        /**
         * 赋值为默认值
         */
        if (mBorderWidthPressed == 0) {
            mBorderWidthPressed = mBorderWidthNormal;
        }
        if (mBorderWidthUnable == 0) {
            mBorderWidthUnable = mBorderWidthNormal;
        }
        if (mBorderColorPressed == 0) {
            mBorderColorPressed = mBorderColorNormal;
        }
        if (mBorderColorUnable == 0) {
            mBorderColorUnable = mBorderColorNormal;
        }

        if (mBackgroundColorNormal == 0 && mBackgroundColorUnable == 0 && mBackgroundColorPressed == 0) {
            if (mBorderColorPressed == 0 && mBorderColorUnable == 0 && mBorderColorNormal == 0) {
                setBackground(getBackground());
            } else {
                setBackground(mStateBackground);
            }
        } else {
            //设置背景资源
            setBackground(mStateBackground);
        }




        //设置边框
        setBorder();



        //设置圆角
        setRadius();

    }

    /**
     * 是否移出view
     *
     * @param x
     * @param y
     * @return
     */
    private boolean isOutsideView(int x, int y) {
        boolean flag = false;
        // Be lenient about moving outside of buttons
        if ((x < 0 - mTouchSlop) || (x >= getWidth() + mTouchSlop) ||
                (y < 0 - mTouchSlop) || (y >= getHeight() + mTouchSlop)) {
            // Outside button
            flag = true;
        }
        return flag;
    }

    /*********************
     * border
     *********************/

    public J4LinearLayout setBorderWidthNormal(int width) {
        this.mBorderWidthNormal = width;
        setBorder(mBackgroundNormal, mBorderColorNormal, mBorderWidthNormal);
        return this;
    }

    public int getBorderWidthNormal() {
        return mBorderWidthNormal;
    }

    public J4LinearLayout setBorderColorNormal(int color) {
        this.mBorderColorNormal = color;
        setBorder(mBackgroundNormal, mBorderColorNormal, mBorderWidthNormal);
        return this;
    }

    public int getBorderColorNormal() {
        return mBorderColorNormal;
    }

    public J4LinearLayout setBorderWidthPressed(int width) {
        this.mBorderWidthPressed = width;
        setBorder(mBackgroundPressed, mBorderColorPressed, mBorderWidthPressed);
        return this;
    }

    public int getBorderWidthPressed() {
        return mBorderWidthPressed;
    }

    public J4LinearLayout setBorderColorPressed(int color) {
        this.mBorderColorPressed = color;
        setBorder(mBackgroundPressed, mBorderColorPressed, mBorderWidthPressed);
        return this;
    }

    public int getBorderColorPressed() {
        return mBorderColorPressed;
    }

    public J4LinearLayout setBorderWidthUnable(int width) {
        this.mBorderWidthUnable = width;
        setBorder(mBackgroundUnable, mBorderColorUnable, mBorderWidthUnable);
        return this;
    }

    public int getBorderWidthUnable() {
        return mBorderWidthUnable;
    }

    public J4LinearLayout setBorderColorUnable(int color) {
        this.mBorderColorUnable = color;
        setBorder(mBackgroundUnable, mBorderColorUnable, mBorderWidthUnable);
        return this;
    }

    public int getBorderColorUnable() {
        return mBorderColorUnable;
    }

    public void setBorderWidth(int normal, int pressed, int unable) {
        this.mBorderWidthNormal = normal;
        this.mBorderWidthPressed = pressed;
        this.mBorderWidthUnable = unable;
        setBorder();
    }

    public void setBorderColor(int normal, int pressed, int unable) {
        this.mBorderColorNormal = normal;
        this.mBorderColorPressed = pressed;
        this.mBorderColorUnable = unable;
        setBorder();
    }

    public void setBorderDashWidth(float dashWidth) {
        this.mBorderDashWidth = dashWidth;
        setBorder();
    }

    public float getBorderDashWidth() {
        return mBorderDashWidth;
    }

    public void setBorderDashGap(float dashGap) {
        this.mBorderDashGap = dashGap;
        setBorder();
    }

    public float getBorderDashGap() {
        return mBorderDashGap;
    }

    public void setBorderDash(float dashWidth, float dashGap) {
        this.mBorderDashWidth = dashWidth;
        this.mBorderDashGap = dashGap;
        setBorder();
    }

    private void setBorder() {
        setBorder(mBackgroundNormal, mBorderColorNormal, mBorderWidthNormal);
        setBorder(mBackgroundPressed, mBorderColorPressed, mBorderWidthPressed);
        setBorder(mBackgroundUnable, mBorderColorUnable, mBorderWidthUnable);
    }

    private void setBorder(GradientDrawable background, int borderColor, int borderWidth) {
        background.setStroke(borderWidth, borderColor, mBorderDashWidth, mBorderDashGap);
    }

    /*********************
     * radius
     ********************/

    public void setCornerRadius(float radius) {
        this.mCornerRadius = radius;
        setRadius();
    }

    public float getCornerRadius() {
        return mCornerRadius;
    }

    public J4LinearLayout setCornerRadiusTopLeft(float topLeft) {
        this.mCornerRadius = -1;
        this.mCornerRadiusTopLeft = topLeft;
        return this;
    }

    public float getCornerRadiusTopLeft() {
        return mCornerRadiusTopLeft;
    }

    public J4LinearLayout setCornerRadiusTopRight(float topRight) {
        this.mCornerRadius = -1;
        this.mCornerRadiusTopRight = topRight;
        return this;
    }

    public float getCornerRadiusTopRight() {
        return mCornerRadiusTopRight;
    }

    public J4LinearLayout setCornerRadiusBottomRight(float bottomRight) {
        this.mCornerRadius = -1;
        this.mCornerRadiusBottomRight = bottomRight;
        return this;
    }

    public float getCornerRadiusBottomRight() {
        return mCornerRadiusBottomRight;
    }

    public J4LinearLayout setCornerRadiusBottomLeft(float bottomLeft) {
        this.mCornerRadius = -1;
        this.mCornerRadiusBottomLeft = bottomLeft;
        return this;
    }

    public float setCornerRadiusBottomLeft() {
        return mCornerRadiusBottomLeft;
    }

    public void setCornerRadius(float topLeft, float topRight, float bottomRight, float bottomLeft) {
        this.mCornerRadius = -1;
        this.mCornerRadiusTopLeft = topLeft;
        this.mCornerRadiusTopRight = topRight;
        this.mCornerRadiusBottomRight = bottomRight;
        this.mCornerRadiusBottomLeft = bottomLeft;
        setRadius();
    }

    private void setRadiusRadii() {
        mBackgroundNormal.setCornerRadii(mBorderRadii);
        mBackgroundPressed.setCornerRadii(mBorderRadii);
        mBackgroundUnable.setCornerRadii(mBorderRadii);
    }

    private void setRadius() {
        if (mCornerRadius >= 0) {
            mBorderRadii[0] = mCornerRadius;
            mBorderRadii[1] = mCornerRadius;
            mBorderRadii[2] = mCornerRadius;
            mBorderRadii[3] = mCornerRadius;
            mBorderRadii[4] = mCornerRadius;
            mBorderRadii[5] = mCornerRadius;
            mBorderRadii[6] = mCornerRadius;
            mBorderRadii[7] = mCornerRadius;
            setRadiusRadii();
            return;
        }

        if (mCornerRadius < 0) {
            mBorderRadii[0] = mCornerRadiusTopLeft;
            mBorderRadii[1] = mCornerRadiusTopLeft;
            mBorderRadii[2] = mCornerRadiusTopRight;
            mBorderRadii[3] = mCornerRadiusTopRight;
            mBorderRadii[4] = mCornerRadiusBottomRight;
            mBorderRadii[5] = mCornerRadiusBottomRight;
            mBorderRadii[6] = mCornerRadiusBottomLeft;
            mBorderRadii[7] = mCornerRadiusBottomLeft;
            setRadiusRadii();
            return;
        }
    }
}